package com.module.framework.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.module.framework.domain.Menu;
import com.module.framework.mapper.MenuMapper;
import com.module.framework.service.MenuService;
import com.module.framework.vo.MenuTreeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<MenuTreeVO> selectPermsByRoleId(long roleId) {
        List<Menu> menus = menuMapper.selectPermsByRoleId(roleId);
        List<MenuTreeVO> sysMenuList = getTreeVOS(menus);
        return sysMenuList;
    }

    public List<MenuTreeVO> selectPermsByUserId(long userId) {
        List<Menu> menus = menuMapper.selectPermsByUserId(userId);
        List<MenuTreeVO> sysMenuList = getTreeVOS(menus);
        return sysMenuList;
    }

    private List<MenuTreeVO> getTreeVOS(List<Menu> menus) {
        List<Menu> menusP = menus.stream().filter(menu -> menu.getParentId() == null).collect(Collectors.toList());
        menus.removeAll(menusP);
        List<MenuTreeVO> sysMenuList = new ArrayList<>();
        for (Iterator<Menu> iterator = menusP.iterator(); iterator.hasNext(); ) {
            Menu next = iterator.next();
            MenuTreeVO menuTreeResponse = new MenuTreeVO();
            menuTreeResponse.setId(next.getId());
            menuTreeResponse.setComponent(next.getComponent());
            menuTreeResponse.setIcon(next.getIcon());
            menuTreeResponse.setName(next.getMenuName());
            menuTreeResponse.setHidden(next.getHidden());
            recursionFn(menus, menuTreeResponse);
            sysMenuList.add(menuTreeResponse);
        }
        return sysMenuList;
    }


    /**
     * @param sysMenus 所有子节点
     * @param menu     父节点
     */
    private void recursionFn(List<Menu> sysMenus, MenuTreeVO menu) {
        List<MenuTreeVO> childList = new ArrayList<>();
        Iterator<Menu> iterator = sysMenus.iterator();
        while (iterator.hasNext()) {
            Menu next = iterator.next();
            if (next.getParentId() == menu.getId()) {
                MenuTreeVO menuTreeResponse = new MenuTreeVO();
                menuTreeResponse.setId(next.getId());
                menuTreeResponse.setName(next.getMenuName());
                menuTreeResponse.setHidden(next.getHidden());
                menuTreeResponse.setComponent(next.getComponent());
                menuTreeResponse.setIcon(next.getIcon());
                childList.add(menuTreeResponse);
            }
        }
        menu.setChildren(childList);
        //sysMenus.removeAll(childList);
        for (MenuTreeVO tChild : childList) {
            recursionFn(sysMenus, tChild);
        }
    }

}
